<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="./css/base.css">
    <style>
        .crumbs {
            font-size: 14px;
            color: #ccc;
        }
    </style>
</head>

<body>
    <div class="container">
        <nav class="navbar navbar-default">
            <div class="container-fluid">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                        data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#" style="display:flex;">

                    </a>
                </div>

                <!-- Collect the nav links, forms, and other content for toggling -->
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">

                    <ul class="nav navbar-nav navbar-right">
                        <li><a href="/">首页</a></li>

                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
                                aria-haspopup="true" aria-expanded="false">分类 <span class="caret"></span></a>
                            <ul class="dropdown-menu" id="cates">

                            </ul>
                        </li>
                        <li><a href="/tags.html">标签</a></li>
                        <li><a href="/about.html">关于我</a></li>
                    </ul>
                </div><!-- /.navbar-collapse -->
            </div><!-- /.container-fluid -->
        </nav>
        <div style="text-align:center;" id="info">


        </div>
        <div class="content">
            <div style="text-align: center;">
                <img id='img' style="height: 200px;" src="http://placehold.it/170x100/ff3333/ffffff?text=like%20you"
                    alt="">
            </div>

            <p id="content">

            </p>
        </div>
        <hr>
        <div class="footer" style="text-align:center;font-size: 14px; color: #999;margin:10px 0;">
            首页 | 主站 | GitHub | Powered by hexo | Theme xoxo
        </div>
    </div>
</body>
<script src="./js/jquery.js"></script>
<script src="./bootstrap/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/layer/3.5.1/layer.js"></script>

<script>
    let url = location.href;
    let reg = /\?id=(.+)\&?/
    let [, id] = Array.from(url.match(reg));
    if (isNaN(parseInt(id))) {
        layer.msg(' 请求错误')
        localStorage.href = '/'
    }
    function loadArticle(id) {
        $.get('http://127.0.0.1:8888/api/detail', { id }, res => {
            console.log(id)
            console.log(res)
            //如果是空对象打回到首页
            if (JSON.stringify(res) === '{}') {
                location.href = '/'
                return;
            }
            let { cat_id, cate_name, content, is_show, img, author, addtime, article } = res;
            //渲染页面
            let htmlStr = `
            <h4 class="crumbs" style="text-align:left;">当前位置：首页 / ${cate_name} / ${article}</h4>
            <h3>${article}</h3>
            <div style="font-size: 14px; color: #999;"> 2021-09-10 /
                分类：${cat_id} / 点赞：666 /作者:${author}
            </div>`
            
            $('#info').html(htmlStr);

            $('#content').html(content);
            if (img) {
                $('#img').attr('src', "http://127.0.0.1:8888/" + img)
            } else {
                //设置默认图片
            }
        }, 'json')
    }
    //渲染头部的分类数据
    function renderCate(cates) {
        let lis = '';
        cates.forEach((cat) => {
            let { cate_id, cate_name } = cat;
            lis += `<li><a href="/cate.html?id=${cate_id}">${cate_name}</a></li>`
        });
        return lis;
    }
    // 获取分类
    function loadCate() {
        // $.ajax此函数返回的是一个promise
        return $.ajax({
            url: 'http://127.0.0.1:8888/api/cate'
        })
    }
    loadArticle(id)
    // 获取本地存储中的分类数据
    let cates = JSON.parse(localStorage.getItem("cates") || '[]');
    // 如果有则读取本地的缓存，否则重新获取在存入本地存储
    if (cates.length) {
        let lis = renderCate(cates)
        $("#cates").html(lis)
    } else {
        loadCate().then(res => {
            let lis = renderCate(res)
            $("#cates").html(lis)
        })
    }
</script>

</html>